home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1997 January / macformat46.iso / Shareware Plus / Developers / Library / Grant's CGI Framework / Grant's CGI Framework / grantscgi / Util / EventUtil.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-20  |  14.2 KB  |  590 lines

  1. /*****
  2.  *
  3.  *    Events.c
  4.  *
  5.  *    This is a support file for "Grant's CGI Framework".
  6.  *    Please see the license agreement that accompanies the distribution package
  7.  *    for licensing details.
  8.  *
  9.  *    Copyright ©1995,1996 by Grant Neufeld
  10.  *    grant@acm.com
  11.  *    http://arpp.carleton.ca/cgi/framework/
  12.  *
  13.  *****/
  14.  
  15. #include "MyConfiguration.h"
  16.  
  17. #if kCompileWithDragNDrop
  18. #include <Drag.h>
  19. #endif
  20.  
  21. #include "constants.h"
  22. #include "globals.h"
  23.  
  24. #include "AboutBox.h"
  25. #include "CustomHandlers.h"
  26. #include "DebugUtil.h"
  27. #include "ErrorUtil.h"
  28. #include "LogUtil.h"
  29. #include "MenuFunc.h"
  30. #include "ProcessUtil.h"
  31. #include "Quit.h"
  32. #include "WindowInt.h"
  33.  
  34. #include "EventUtil.h"
  35.  
  36.  
  37. /***  LOCAL PROTOTYPES ***/
  38.  
  39. static     void    doKeyPress    ( EventRecord * );
  40.  
  41.  
  42. /***  FUNCTIONS  ***/
  43.  
  44. /* 
  45.     ->message    long    the high-level event class to which the event belongs
  46.     ->when        long    time when event was posted (in ticks since system startup)
  47.     ->where        Point    the high-level event ID (cast as OSType)
  48.     ->modifiers    int        state of modifier keys and mouse button at event time */
  49. void
  50. doHighLevelEvent ( EventRecord *theEvent )
  51. {
  52.     OSErr        theErr;
  53.     
  54.     /* reset 'quit on idle time' timer */
  55.     ResetQuitIdleTimer();
  56.     
  57.     theErr = AEProcessAppleEvent ( theEvent );
  58.     if ( theErr != noErr )
  59.     {
  60.         /* you might want to add error handling here.
  61.             However, I advise against putting user interaction handling here,
  62.             it can confuse the user. You might try using an assertion. */
  63.         ErrorSystemHandler ( theErr, false );
  64.     }
  65. } /* doHighLevelEvent */
  66.  
  67.  
  68. /**  USER INTERFACE  **/
  69. #pragma mark -
  70. #if kCompileWithForeground
  71.  
  72. /* MOUSE HANDLING */
  73. /* 
  74.     ->message    long    undefined
  75.     ->when        long    time when event was posted (in ticks since system startup)
  76.     ->where        Point    cursor location at event time
  77.     ->modifiers    int        state of modifier keys and mouse button at event time
  78.                         and wether the mouse-down caused the application to come to the foreground
  79.     IM-MTE: 2-34 */
  80. void
  81. doMouseDown ( EventRecord *theEvent )
  82. {
  83.     WindowPtr     theWindow;
  84.     short          where;
  85.     Boolean        onItem;
  86.     #if kCompileWithDragNDrop
  87.     OSErr        theErr;
  88.     #endif
  89.     
  90.     #if kCompileWithModelessDialogs
  91.     /* modeless dialogs */
  92.     Boolean        dialogEvent;
  93.     Boolean        dialogResult;
  94.     DialogPtr    theDialog;
  95.     short        itemHit;
  96.     window_type    theWindowType;
  97.     
  98.     /* determine whether the event is for a modeless dialog */
  99.     dialogEvent = IsDialogEvent ( theEvent );
  100.     if ( dialogEvent )
  101.     {
  102.         dialogResult = DialogSelect ( theEvent, &theDialog, &itemHit );
  103.         
  104.         theWindowType = WindowType ( (WindowPtr)theDialog );
  105.         switch ( theWindowType )
  106.         {
  107.             case Window_about :
  108.                 /* the About dialog is the only dialog in the framework that can
  109.                     receive events from the normal event loop - all others either
  110.                     have their own event handling or are custom dialogs which are
  111.                     handled by the default case */
  112.                 AboutHandleEvent ( theEvent, theDialog, itemHit );
  113.                 break;
  114.             
  115.             default :
  116.                 #if kCompileWithApplicationWindows
  117.                 CustomDialogMouseDown ( theEvent, theDialog, itemHit );
  118.                 #endif
  119.                 break;
  120.         }
  121.     }
  122.     else
  123.     {
  124.     #endif
  125.     
  126.         where = FindWindow ( theEvent->where, &theWindow );
  127.         switch ( where ) /* where the mouse down occurred */
  128.         {
  129.             case inDesk :
  130.                 break;
  131.             
  132.             case inMenuBar :
  133.                 adjustMenus    ();
  134.                 doMenu ( MenuSelect(theEvent->where), theEvent->modifiers );
  135.                 break;
  136.             
  137.             case inSysWindow :
  138.                 /* click in a desk accessory window */
  139.                 SystemClick ( theEvent, theWindow );
  140.                 break;
  141.         
  142.             case inContent :
  143.                 /* content region of a window */
  144.                 if ( (theWindow != FrontWindow()) || !(((WindowPeek)theWindow)->hilited) )
  145.                 {
  146.                     switch ( WindowType(FrontWindow()) )
  147.                     {
  148.                         case Window_dlgModal :
  149.                         case Window_dlgMoveableModal :
  150.                             /* beep because can't switch from modal dialog */
  151.                             SysBeep ( 30 );
  152.                             break;
  153.                         
  154.                         case Window_UNKNOWN :
  155.                             my_assert ( kForceAssert, "\pdoMouseDown: Unknown window type" );
  156.                         
  157.                         case Window_about :
  158.                         case Window_DA :
  159.                         default :
  160.                             SelectWindow ( theWindow );
  161.                             break;
  162.                     }
  163.                 }
  164.                 else
  165.                 {
  166.                     onItem = false;
  167.                     WindowContentClick ( theWindow, theEvent, &onItem );
  168.                     
  169.                     #if kCompileWithDragNDrop
  170.                     if ( gHasDragNDrop && onItem && (WaitMouseMoved(theEvent->where)) )
  171.                     {
  172.                         theErr = CustomDoStartDrag ( theEvent, theWindow );
  173.                     }
  174.                     #endif
  175.                 }
  176.                 break;
  177.     
  178.             case inDrag : /* title bar region of a window */
  179.                 switch ( WindowType(theWindow) )
  180.                 {
  181.                     case Window_dlgMoveableModal :
  182.                         if ( (theWindow == FrontWindow()) || (theEvent->modifiers & cmdKey) )
  183.                         {
  184.                             DragWindow ( theWindow, theEvent->where, &gGrayRgnRect );
  185.                         }
  186.                         else
  187.                         {
  188.                             /* beep because can't switch from moveable modal dialog */
  189.                             SysBeep ( 30 );
  190.                         }
  191.                         break;
  192.                     
  193.                     default :
  194.                         /* if the command key is pressed, don't activate the window, just drag it */
  195.                         if ( !(theEvent->modifiers & cmdKey) )
  196.                         {
  197.                             SelectWindow ( theWindow );
  198.                         }
  199.                         
  200.                         DragWindow ( theWindow, theEvent->where, &gGrayRgnRect );
  201.                         break;
  202.                 }
  203.                 break;
  204.         
  205.             case inGrow : /* grow box region of a window (lower right hand corner) */
  206.                 WindowGrow ( theWindow, theEvent->where );
  207.                 break;
  208.                 
  209.             case inZoomIn:
  210.             case inZoomOut: /* zoom box of a window (upper right hand corner) */
  211.                 if ( TrackBox(theWindow, theEvent->where, where) )
  212.                 {
  213.                     WindowZoomBox ( theWindow, where );
  214.                 }
  215.                 break;
  216.         
  217.             case inGoAway : /* close box of a window (upper left hand corner) */
  218.                 if ( TrackGoAway ( theWindow, theEvent->where ) )
  219.                 {
  220.                     WindowClose ( theWindow, theEvent->modifiers, true );
  221.                 }
  222.                 break;
  223.         }
  224.         
  225.     #if kCompileWithModelessDialogs
  226.     }
  227.     #endif
  228. } /* doMouseDown */
  229.  
  230.  
  231. /* 
  232.     ->message    long    undefined
  233.     ->when        long    time when event was posted (in ticks since system startup)
  234.     ->where        Point    cursor location at event time
  235.     ->modifiers    int        state of modifier keys and mouse button at event time */
  236. void
  237. doMouseUp ( EventRecord *theEvent )
  238. {
  239.     #if kCompileWithModelessDialogs
  240.     /* modeless dialogs */
  241.     Boolean        dialogEvent;
  242.     Boolean        dialogResult;
  243.     DialogPtr    theDialog;
  244.     short        itemHit;
  245.     
  246.     /* determine whether the event is for a modeless dialog */
  247.     dialogEvent = IsDialogEvent ( theEvent );
  248.     if ( dialogEvent )
  249.     {
  250.         dialogResult = DialogSelect ( theEvent, &theDialog, &itemHit );
  251.     }
  252.     else
  253.     {
  254.         /* all other mouse stuff should have been handled in doMouseDown */
  255.     }
  256.     #endif
  257. } /* doMouseUp */
  258.  
  259.  
  260. /*  Keyboard Events  */
  261. #pragma mark -
  262.  
  263. /* 
  264.     ->message    long    char and virtual key codes in low-order word.
  265.                         ADB address in low byte of high-order word
  266.     ->when        long    time when event was posted (in ticks since system startup)
  267.     ->where        Point    cursor location at event time
  268.     ->modifiers    int        state of modifier keys and mouse button at event time
  269.     IM-MTE 2-44 */
  270. void
  271. doKeyDown ( EventRecord *theEvent )
  272. {
  273.     char    theChar;
  274.     
  275.     /* reset 'quit on idle time' timer */
  276.     ResetQuitIdleTimer();
  277.     
  278.     /* the character being pressed is in the message of the event record */
  279.     theChar = theEvent->message & charCodeMask;
  280.     
  281.     if ( theEvent->modifiers & cmdKey )
  282.     {
  283.         /* if the command key is being held down, treat as menu request */
  284.         adjustMenus    ();
  285.         doMenu ( MenuKey (theChar), theEvent->modifiers );
  286.     }
  287.     else
  288.     {
  289.         /* handle regular keypress */
  290.         doKeyPress ( theEvent );
  291.     }
  292. } /* doKeyDown */
  293.  
  294.  
  295. /* 
  296.     ->message    long    char and virtual key codes in low-order word.
  297.                         ADB address in low byte of high-order word
  298.     ->when        long    time when event was posted (in ticks since system startup)
  299.     ->where        Point    cursor location at event time
  300.     ->modifiers    int        state of modifier keys and mouse button at event time */
  301. void
  302. doAutoKey ( EventRecord *theEvent )
  303. {
  304.     if ( theEvent->modifiers & cmdKey )
  305.     {
  306.         /* don't do anything. Command keys should not auto-repeat. */
  307.     }
  308.     else
  309.     {
  310.         /* handle regular keypress */
  311.         doKeyPress ( theEvent );
  312.     }
  313. } /* doAutoKey */
  314.  
  315.  
  316. /* 
  317.     ->message    long    char and virtual key codes in low-order word.
  318.                         ADB address in low byte of high-order word
  319.     ->when        long    time when event was posted (in ticks since system startup)
  320.     ->where        Point    cursor location at event time
  321.     ->modifiers    int        state of modifier keys and mouse button at event time */
  322. void
  323. doKeyUp ( EventRecord *theEvent )
  324. {
  325. #pragma unused (theEvent)
  326.     /* key action should have been handled by keyDown and autoKey */
  327. } /* doKeyUp */
  328.  
  329.  
  330. /* handle regular keypress
  331.     ->message    long    char and virtual key codes in low-order word.
  332.                         ADB address in low byte of high-order word
  333.     ->when        long    time when event was posted (in ticks since system startup)
  334.     ->where        Point    cursor location at event time
  335.     ->modifiers    int        state of modifier keys and mouse button at event time
  336.     IM-MTE: 2-44,45 */
  337. static void
  338. doKeyPress ( EventRecord *theEvent )
  339. {
  340.     /* If you add windows that allow for text entry or you accept non-command-key
  341.         keyboard input, you need to support this function. */
  342.     #if kCompileWithKeyboardEvents
  343.     CustomKeyPress ( theEvent );
  344.     #else
  345. #pragma unused (theEvent)
  346.     #endif
  347. } /* doKeyPress */
  348.  
  349.  
  350. /**  Other Events  **/
  351. #pragma mark -
  352.  
  353. /* Handle activate event - a window has been brought to front, or a window
  354.     that was in front is no longer.
  355.     ->message    long    window ptr for window to de/activate
  356.     ->when        long    time when event was posted (in ticks since system startup)
  357.     ->where        Point    cursor location at event time
  358.     ->modifiers    int        state of modifier keys and mouse button at event time
  359.                         and whether window should be activated or deactivated
  360.     IM-MTE: 2-50-55 */
  361. void
  362. doActivateEvent ( EventRecord *theEvent )
  363. {
  364.     WindowPtr    theWindow;
  365.     Boolean        becomingActive;
  366.     
  367.     /* reset 'quit on idle time' timer */
  368.     ResetQuitIdleTimer();
  369.     
  370.     theWindow        = (WindowPtr) theEvent->message;
  371.     becomingActive    = (theEvent->modifiers & activeFlag) != nil;
  372.     
  373.     switch ( WindowType(theWindow) )
  374.     {
  375.         case Window_none :
  376.             break;
  377.         
  378.         case Window_UNKNOWN :
  379.             my_assert ( kForceAssert, "\pdoActivateEvent: Unknown window type" );
  380.             break;
  381.         
  382.         default :
  383.             WindowActivate ( theWindow, becomingActive, theEvent );
  384.             break;
  385.     }
  386. } /* doActivateEvent */
  387.  
  388.  
  389. /* A window needs to be updated.
  390.     ->message    long    window ptr for window to update
  391.     ->when        long    time when event was posted (in ticks since system startup)
  392.     ->where        Point    cursor location at event time
  393.     ->modifiers    int        state of modifier keys and mouse button at event time
  394.     IM-MTE: 2-47-50 */
  395. void
  396. doUpdateEvent ( EventRecord *theEvent )
  397. {
  398.     WindowPtr    theWindow;
  399.     
  400.     theWindow = (WindowPtr) (theEvent->message);
  401.     if ( theWindow != NULL )
  402.     {
  403.         WindowUpdate ( theWindow );
  404.     }
  405. } /* doUpdateEvent */
  406.  
  407.  
  408. /*
  409.     ->message    long    window ptr for window to de/activate
  410.     ->when        long    time when event was posted (in ticks since system startup)
  411.     ->where        Point    cursor location at event time
  412.     ->modifiers    int        state of modifier keys and mouse button at event time
  413.                         and whether window should be activated or deactivated
  414.     IM-MTE: 2-58-62 */
  415. void
  416. doOsEvt ( const EventRecord *theEvent )
  417. {
  418.     WindowPtr    theWindow;
  419.     
  420.     switch ( (theEvent->message >> 24) & 0x000000FF )    /* should have a constant here */
  421.     {
  422.         case suspendResumeMessage :
  423.             gFrontProcess = theEvent->message & resumeFlag;
  424.             
  425.             theWindow = FrontWindow ();
  426.             
  427.             if ( gFrontProcess )
  428.             {
  429.                 /* resume event */
  430.                 
  431.                 if ( theEvent->message & convertClipboardFlag )
  432.                 {
  433.                     /* convert the clipboard */
  434.                 }
  435.                 
  436.                 switch ( WindowType(theWindow) )
  437.                 {
  438.                     case Window_none :
  439.                         break;
  440.                     
  441.                     case Window_UNKNOWN :
  442.                         my_assert ( kForceAssert, "\pdoOsEvt: Unknown window type" );
  443.                     
  444.                     default :
  445.                         WindowActivate ( theWindow, true, theEvent );
  446.                         break;
  447.                 }
  448.                 
  449.                 /* reset 'quit on idle time' timer */
  450.                 ResetQuitIdleTimer();
  451.             }
  452.             else
  453.             {
  454.                 /* suspend event */
  455.                 /* convert the clipboard, if needed, for export */
  456.                 
  457.                 switch ( WindowType(theWindow) )
  458.                 {
  459.                     case Window_none :
  460.                         break;
  461.                     
  462.                     case Window_UNKNOWN :
  463.                         my_assert ( kForceAssert, "\pdoOsEvt: Unknown window type" );
  464.                     
  465.                     default :
  466.                         WindowActivate ( theWindow, false, theEvent );
  467.                         break;
  468.                 }
  469.             }
  470.             break;
  471.         
  472.         case mouseMovedMessage : /* IM-MTE: 2-62-67 */
  473.             /* you may want to adjust your cursor and mouse region here */
  474.             break;
  475.     }
  476. } /* doOsEvt */
  477.  
  478.  
  479. /* Handle 'bad' disk mounts. Just call the system DIBadMount call.
  480.     ->message    long    drive number in low-order word, file manager result code in high-order word
  481.     ->when        long    time when event was posted (in ticks since system startup)
  482.     ->where        Point    cursor location at event time
  483.     ->modifiers    int        state of modifier keys and mouse button at event time
  484.     IM-MTE:2-55,56 */
  485. void
  486. doDiskEvt ( EventRecord *theEvent )
  487. {
  488.     Point    aPoint;
  489.     OSErr    theErr;
  490.     
  491.     /* if disk mount was unsuccessful */
  492.     if ( HiWord(theEvent->message) != noErr )
  493.     {
  494.         /* load disk initialization manager */
  495.         DILoad ();
  496.         
  497.         aPoint.v    = 120;
  498.         aPoint.h    = 120;
  499.         theErr        = DIBadMount ( aPoint, theEvent->message );
  500.         
  501.         /* unload disk initialization manager */
  502.         DIUnload ();
  503.     }
  504. } /* doDiskEvt */
  505.  
  506. #endif    /* kCompileWithForeground */
  507.  
  508. /**  Idle Time  **/
  509. #pragma mark -
  510.  
  511. /* 
  512.     ->message    long    undefined
  513.     ->when        long    time when event was posted (in ticks since system startup)
  514.     ->where        Point    cursor location at event time
  515.     ->modifiers    int        state of modifier keys and mouse button at event time
  516.     IM-MTE: 2-57,58, 6-79,80 */
  517. void
  518. doIdle ( EventRecord *theEvent )
  519. {
  520.     #if kCompileWithForeground
  521.     WindowPtr    theWindow;
  522.     short        itemHit;
  523.     Boolean        theResult;
  524.     #endif
  525.     
  526.     #if kCompileWithLogSupport
  527.     LogFileFlush ();
  528.     #endif
  529.     
  530.     #if kCompileWithThreadsOptional
  531.     if ( gHasThreadMgr )
  532.     {
  533.     #endif
  534.         ThreadYield ( nil, false );
  535.     #if kCompileWithThreadsOptional
  536.     }
  537.     #if kCompileWithDeferredTask
  538.     else
  539.     {
  540.         CustomDeferredTask ();
  541.     }
  542.     #endif /* kCompileWithDeferredTask */
  543.     #endif
  544.     
  545.     #if kCompileWithQuitOnLongIdle
  546.     if ( gDoIdleQuit && (gTimeLastAction > nil) &&
  547.         ((gTimeLastAction + gIdleTimeToQuit) < TickCount()) )
  548.     {
  549.         gQuit = true;
  550.     }
  551.     #endif
  552.     
  553.     #if kCompileWithForeground
  554.     if ( gFrontProcess )
  555.     {
  556.         theWindow = FrontWindow ();
  557.         
  558.         switch ( WindowType(theWindow) )
  559.         {
  560.             case Window_about :
  561.             case Window_none :
  562.                 /* don't do anything */
  563.                 break;
  564.             
  565.             case Window_dlgModal :
  566.             case Window_dlgMoveableModal :
  567.                 /* IM:MTB 2-29,30 */
  568.                 theResult = DialogSelect ( theEvent, (DialogPtr *)&theWindow, &itemHit );
  569.                 break;
  570.             
  571.             #if kCompileWithApplicationWindows
  572.             case Window_Application :
  573.                 CustomWindowIdle ( theWindow );
  574.                 break;
  575.             #endif
  576.             
  577.             case Window_UNKNOWN :
  578.                 my_assert ( kForceAssert, "\pdoIdle: unknown window type" );
  579.                 break;
  580.             
  581.             default :
  582.                 break;
  583.         }
  584.     }
  585.     #endif    /* kCompileWithForeground */
  586. } /* doIdle */
  587.  
  588.  
  589. /*****  EOF  *****/
  590.